//
//  ZFCustomControlView.m
//  ZFPlayer_Example
//
//  Created by 紫枫 on 2019/6/5.
//  Copyright © 2019 紫枫. All rights reserved.
//


#import "SliderView_icon.h"
#import "UXUtilities.h"
@interface SliderView_icon ()<ZFSliderViewDelegate>
/// 加入授课包
//@property (nonatomic, strong) UIButton *addBtn;
@end

@implementation SliderView_icon

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

        [self addAllSubViews];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat min_margin = 5;
    CGFloat Icon_w = 20;
    CGFloat Icon_H = 20;
    CGFloat slider_H = 30;
    CGFloat min_view_w = self.bounds.size.width;
    CGFloat min_view_h = self.bounds.size.height;
    
    self.leftIcon.frame = CGRectMake(0, (min_view_h - Icon_H)/2, Icon_w, Icon_H);
    self.slider.frame = CGRectMake(Icon_w + min_margin, (min_view_h - slider_H)/2, min_view_w - Icon_w - Icon_w - min_margin - min_margin, slider_H);
    self.rightIcon.frame = CGRectMake(CGRectGetMaxX(self.slider.frame) + min_margin, (min_view_h - Icon_H)/2, Icon_w, Icon_H);

}

- (void)layoutControllerViews {
    [self layoutIfNeeded];
    [self setNeedsLayout];
}

- (void)dealloc {
}

/// 添加所有子控件
- (void)addAllSubViews {
    [self addSubview:self.leftIcon];
    [self addSubview:self.rightIcon];
    [self addSubview:self.slider];
}

- (void)showViewLeftIcon:(NSString *)leftIcon rightIcon:(NSString *)rightIcon{
//    [self showViewLeftIcon:leftIcon rightIcon:rightIcon sliderChangingBlock:nil];
    self.leftIcon.image = UXUtilities_Image(leftIcon);
    self.rightIcon.image = UXUtilities_Image(rightIcon);
}

- (void)showViewLeftIcon:(NSString *)leftIcon rightIcon:(NSString *)rightIcon sliderChangingBlock:(SliderValueChangingBlock )sliderChangingBlock{
    self.leftIcon.image = UXUtilities_Image(leftIcon);
    self.rightIcon.image = UXUtilities_Image(rightIcon);
    if (sliderChangingBlock != nil) {
        self.sliderBlock = [sliderChangingBlock copy];
    }

}
- (UIImageView *)leftIcon {
    if (!_leftIcon) {
        _leftIcon = [[UIImageView  alloc] initWithFrame:CGRectZero];
        _leftIcon.image = UXUtilities_Image(@"icon_add");
    }
    return _leftIcon;
}
- (UIImageView *)rightIcon {
    if (!_rightIcon) {
        _rightIcon = [[UIImageView  alloc] initWithFrame:CGRectZero];
        _rightIcon.image = UXUtilities_Image(@"icon_share");
    }
    return _rightIcon;
}

- (ZFSliderView *)slider {
    if (!_slider) {
        _slider = [[ZFSliderView alloc] init];
        _slider.delegate = self;
        _slider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.8];
        _slider.bufferTrackTintColor  = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
        _slider.minimumTrackTintColor = [UIColor whiteColor];
        [_slider setThumbImage:UXUtilities_Image(@"ZFPlayer_slider") forState:UIControlStateNormal];
        _slider.sliderHeight = 2;
    }
    return _slider;
}

#pragma mark - ZFSliderViewDelegate

- (void)sliderTouchBegan:(float)value {
    self.slider.isdragging = YES;
}

- (void)sliderTouchEnded:(float)value {

    if (self.sliderBlock) self.sliderBlock(value, self.slider.isForward);
    self.slider.isdragging = false;
}

- (void)sliderValueChanged:(float)value {

    self.slider.isdragging = YES;
    if (self.sliderBlock) self.sliderBlock(value, self.slider.isForward);
}

- (void)sliderTapped:(float)value {
    [self sliderTouchEnded:value];

}

@end
